<?php
class Book extends MY_Controller{
	function index(){
		$this->load->model("Books");
		$get_data = $this->input->get();
		$conditions = array();
		if(!empty($get_data['grade'])){
			$conditions['grade'] = $get_data['grade'];
		}
		if(!empty($get_data['semester'])){
			$conditions['semester'] = $get_data['semester'];
		}
		if(!empty($get_data['subject_id'])){
			$conditions['subject_id'] = $get_data['subject_id'];
		}
		$res = $this->Books->get_books($conditions);
		$code = 200;
		if($res){
			$msg = "获取数据成功";
		}else{
			$msg = "暂时没有课本";
		}
		$result['code'] = $code;
		$result['msg'] = $msg;
		$result['data'] = $res;
		echo json_encode($result);
	}

	/*function chapter_list(){
		$post_data = $this->input->post();
		$post_data['book_id'] = 2777;
		$post_data['parent_id'] = 0;
		if(empty($post_data['book_id'])){
			$code = 10014;
		}else{
			$this->load->model("Books");
			$result = $this->Books->get_book_chapter($post_data['book_id'], $post_data['parent_id']);
			echo json_encode($result);
		}
	}*/

	function chapter_list($book_id = 0, $limit = 0){
		$book_info = get_field_by_where("b_book", "*", "id='{$book_id}'");
		if(!$book_info){
			show_message($_SERVER['HTTP_REFERER'], "课本不存在");exit;
		}
		$this->load->model("Books");
		$conditions['book_id'] = $book_id;
		$book_info['chapter_list'] = $this->Books->get_book_chapter_list($limit, $this->offset, $conditions);
		echo json_encode($book_info);
	}
}